<?php
/**
* This is a representation of a game in DB
* @author Juan G. Bernal <juancho2986@gmail.com>
* @version 1.0
* @package Game
*/
class Game
{
    private $dbinterface;

    /**
    * Constructor function
    */
    function __construct()
    {
        $this->dbinterface = new DBInterface();
    }

    /**
    * Insert a new game in th DB
    * @param Array $data An associative array with the game data
    * @return Bool Indicates if the insertion was succesful
    */
    function create($data)
    {
        $data['gme_date'] = str_replace(".","-",$data['gme_date']);
        return $this->dbinterface->exec_insert($data,'game');
    }

     /**
    * Modify a single field of one game
    * @param String $fld_name Field name
    * @param String $fld_value Field value
    * @param Integer $id Id of the game to be modified
    */
    function modify($fld_name,$fld_value,$id)
    {
        $data = array();
        if($fld_name=='gme_date')
            $fld_value = str_replace(".","-",$fld_value);
        $data[$fld_name] = $fld_value;
        $this->dbinterface->exec_update($data,'game','gme_id='.$id);
    }

    /**
    * Delete a game
    * @param Integer $id Id of the game to be deleted
    */
    function remove($id)
    {
        $this->dbinterface->exec_delete('game','gme_id='.$id);
    }

    /**
    * Retrieve the information of one game
    * @param Integer $id Id of the game
    * @return Array Array with the game data
    */
    function getDataGame($id)
    {
        $fields = array('gme_id','to_char(gme_date,\'YYYY.MM.DD\') as gme_date','to_char(gme_time,\'HH24:MI\') as gme_time',
                        'rnd_name as gme_round','gme_rnd_id','plc_name as gme_place','gme_plc_id','gme_del_id_1','gme_del_id_2',
                        'gme_referee_1','gme_referee_2','gme_gender');
        $table = array('game','round_wp','place');
        $criteria = 'gme_id='.$id.' and gme_rnd_id=rnd_id and gme_plc_id=plc_id';
        $result_set = $this->dbinterface->exec_select($fields,$table,$criteria);
        $del1 = $this->dbinterface->exec_select(array('del_name'),'delegation','del_id='.$result_set[0]['gme_del_id_1']);
        $del2 = $this->dbinterface->exec_select(array('del_name'),'delegation','del_id='.$result_set[0]['gme_del_id_2']);
        $ref1 = $this->dbinterface->exec_select(array('(usu_first_name || \' \' || usu_last_name) as name'),'user_wp','usu_id='.$result_set[0]['gme_referee_1']);
        $ref2 = $this->dbinterface->exec_select(array('(usu_first_name || \' \' || usu_last_name) as name'),'user_wp','usu_id='.$result_set[0]['gme_referee_2']);
        $result_set[0]['del1'] = $del1[0]['del_name'];
        $result_set[0]['del2'] = $del2[0]['del_name'];
        $result_set[0]['ref1'] = $ref1[0]['name'];
        $result_set[0]['ref2'] = $ref2[0]['name'];
        $time = explode(":",$result_set[0]['gme_time']);
        $result_set[0]['hour'] = $time[0];
        $result_set[0]['min'] = $time[1];
        return $result_set[0];
    }

    /**
    * Retrieve a list of games accord of search criteria
    * @param String $name A part or complete name of one delegation in one game, this is optional
    * @param Integer $limit Limit clause for the SQL query, this is optional
    * @param Integer $offset Offset clause for the SQL query, this is optional
    * @return Array Bidimensional array with the games data
    */
    function getList($name='',$limit=0,$offset=0)
    {
        $fields = array('gme_id','to_char(gme_date,\'YYYY.MM.DD\') as gme_date','rnd_name as gme_round','gme_del_id_1','gme_del_id_2','gme_gender');

        if($name!='')
        {
            $criteria = "(gme_del_id_1=del_id or gme_del_id_2=del_id) and del_name ilike '%".$name."%'";
            $criteria .= " and gme_rnd_id=rnd_id";
            $table = array('game','delegation','round_wp');
        }
        else
        {
            $criteria = 'gme_rnd_id=rnd_id';
            $table = array('game','round_wp');
        }

        if($limit!=0)
            $criteria .= " limit ".$limit;
        if($limit!=0 && $offset!=0)
            $criteria .= " offset ".$offset;

        $result_set = $this->dbinterface->exec_select($fields,$table,$criteria);
        if($result_set==0)
            return 0;

        foreach($result_set as $i => $game)
        {
            $del1 = $this->dbinterface->exec_select(array('del_name'),'delegation','del_id='.$game['gme_del_id_1']);
            $del2 = $this->dbinterface->exec_select(array('del_name'),'delegation','del_id='.$game['gme_del_id_2']);
            $result_set[$i]['del1'] = $del1[0]['del_name'];
            $result_set[$i]['del2'] = $del2[0]['del_name'];
        }
        return $result_set;
    }

    /**
    * Validate if there is a game in a place, date and time given
    * @param String $time The time
    * @param String $place The place
    * @param String $date The date
    * @return Bool Indicates if there is games
    */
    function isScheduled($time,$place,$date)
    {
        $criteria = "gme_date='".$date."' and gme_plc_id=".$place." and gme_time='".$time."'";
        $games = $this->dbinterface->exec_select(array('*'),'game',$criteria);
        if($games==0)
            return false;
        else
            return true;
    }

    /**
    * Validate if a delegation will play a game in a date and time given
    * @param Integer $del Id of the delegation
    * @param String $time The time
    * @param String $date The date
    * @param String $gender The gender
    * @return Bool Indicates if the delegation is free to play
    */
    function freeDelegation($del,$time,$date,$gender)
    {
        $criteria = "gme_date='".$date."' and gme_time='".$time."' and ";
        $criteria .= "(gme_del_id_1=".$del." or gme_del_id_2=".$del.") and ";
        $criteria .= "gme_gender='".$gender."'";
        $games = $this->dbinterface->exec_select(array('*'),'game',$criteria);
        if($games==0)
            return true;
        else
            return false;
    }

    /**
    * Validate if a delegation had scheduled a game for the same round
    * @param Integer $del Id of the delegation
    * @param Integer $round Id of the round
    * @param String $gender The gender
    * @return Bool Indicates if the delegation has games for the same round
    */
    function sameRound($del,$round,$gender)
    {
        $criteria = "gme_rnd_id=".$round." and (gme_del_id_1=".$del." or gme_del_id_2=".$del.") and ";
        $criteria .= "gme_gender='".$gender."'";
        $games = $this->dbinterface->exec_select(array('*'),'game',$criteria);
        if($games==0)
            return false;
        else
            return true;
    }

    /**
    * Validate if a game has events associated
    * @param Integer $id Id of the game
    * @return Bool Indicates if the game has events associated
    */
    function hasEvent($id)
    {
        $events = $this->dbinterface->exec_select(array('*'),'event_game','eve_gme_id='.$id);
        if($events==0)
            return false;
        else
            return true;
    }
}
?>